/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;

import ro.dta.idbi.interfaces.ITransformationResult;
import ro.dta.idbi.interfaces.ui.IHistoryPanel;
import ro.dta.idbi.interfaces.ui.IMainFrame;

/**
 * History panel containing the command history for a given project
 * 
 * @author Tudor Dobrila
 * 
 */
public class HistoryPanel extends JPanel implements IHistoryPanel {

	private static final long serialVersionUID = -912494458484882541L;

	/**
	 * Parent frame of the application
	 */
	protected final transient IMainFrame parentFrame;

	/**
	 * List model of the command list
	 */
	private final transient DefaultListModel<String> listModel;

	/**
	 * History panel containing the command history for the currently open
	 * project
	 * 
	 * @param parentFrame
	 *            Parent frame of the application
	 */
	public HistoryPanel(final IMainFrame parentFrame) {
		super();
		this.parentFrame = parentFrame;
		setLayout(new GridBagLayout());

		final GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.BOTH;

		final JTitleLabel tLabel = new JTitleLabel("Command History");
		constr.ipady = 15;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		constr.gridx = 0;
		constr.gridy = 0;
		add(tLabel, constr);

		listModel = new DefaultListModel<String>();
		final JList<String> historyList = new JList<String>(listModel) {
			private static final long serialVersionUID = 1L;

			public String getToolTipText(final MouseEvent evt) {
				int index = locationToIndex(evt.getPoint());
				String result = "";
				if (index != -1) {
					Object item = getModel().getElementAt(index);
					result = item.toString();
				}
				return result;
			}
		};
		constr.ipady = 0;
		constr.weightx = 1.0;
		constr.weighty = 1.0;
		constr.gridx = 0;
		constr.gridy = 1;
		add(historyList, constr);
	}

	@Override
	public void addCommand(final ITransformationResult command) {
		for (String s : command.getTransCmd()) {
			listModel.addElement(s);
		}
	}

	@Override
	public void removeCommand(String command) {
		listModel.removeElement(command);
	}

}
